import {
  getLocalStorage,
  setLocalStorage,
  removeLocalStorage
} from '@/assets/js/localStorage'
import api from '@/api/index'

/**
 * 登录判定
 * @param {path} 记录当前路径
 * @param {next} 登陆后执行的函数next()
 */
export const isLogin = (path, next) => {
  if (path == null) return
  setLocalStorage('authUrl', path)
  const loginStatus = getLocalStorage('loginStatus') // 获取本地登录状态
  const date = +new Date() // 获取当前时间戳
  const loginDate = getLocalStorage('loginDate') // 获取上次登录时间戳
  if (loginStatus !== null) {
    if (date - parseInt(loginDate) >= 1200000) { // 当前登陆时间与上次登陆时间的间隔大于两小时，重新登录
      removeLocalStorage('loginStatus') // 重置登录状态
      removeLocalStorage('loginDate') // 重置登录时间
      api.accredit
        .accreditLogin('http://api.dawei.pwd5.cn/wap/#/author')
    } else {
      next()
    }
  } else {
    api.accredit
      .accreditLogin('http://api.dawei.pwd5.cn/wap/#/author')
  }
}
